package ui.changeweeklydetails;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

import ui.AddSubTimeDialog;
import ui.changeweeklydetails.TimePanel.TimePanelListener;
import entity.general.MinTime;
import entity.general.Week;
import entity.general.Week.Day;

public class ChangeWeeklyDetailsDialog3 extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private BarPanel mondayBarPanel;
	private TimePanel mondayTimePanel;
	private BarPanel tuesdayBarPanel;
	private TimePanel tuesdayTimePanel;
	private TimePanel wednesdayTimePanel;
	private BarPanel wednesdayBarPanel;
	private TimePanel thursdayTimePanel;
	private BarPanel thursdayBarPanel;
	private BarPanel fridayBarPanel;
	private TimePanel fridayTimePanel;
	private TimePanel saturdayTimePanel;
	private BarPanel saturdayBarPanel;
	private TimePanel sundayTimePanel;
	private BarPanel sundayBarPanel;
	
	List <TimePanel> timePanels = new ArrayList <>();
	List <BarPanel> barPanels = new ArrayList <>();

	static public enum Status {OK, CANCEL};
	Status status = Status.CANCEL;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			ChangeWeeklyDetailsDialog3 dialog = new ChangeWeeklyDetailsDialog3();
			Week week = new Week();
			week.set(Day.MONDAY, new MinTime(3, 30));
			week.set(Day.TUESDAY, new MinTime(1, 30));
			week.set(Day.WEDNESDAY, new MinTime(0, 30));
			week.set(Day.THURSDAY, new MinTime(6, 30));
			week.set(Day.FRIDAY, new MinTime(8, 30));
			week.set(Day.SATURDAY, new MinTime(0, 0));
			week.set(Day.SUNDAY, new MinTime(0, 30));
			dialog.setTimes(week);
			dialog.disableDay(Day.SATURDAY);
			dialog.disableDay(Day.SUNDAY);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public ChangeWeeklyDetailsDialog3() {
		this.setResizable(false);
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setLayout(new FlowLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		{
			JPanel mondayPanel = new JPanel();
			mondayPanel.setBorder(new LineBorder(new Color(0, 0, 0)));
			contentPanel.add(mondayPanel);
			GridBagLayout gbl_mondayPanel = new GridBagLayout();
			gbl_mondayPanel.columnWidths = new int[]{80, 0};
			gbl_mondayPanel.rowHeights = new int[]{32, 0, 0, 0};
			gbl_mondayPanel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
			gbl_mondayPanel.rowWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
			mondayPanel.setLayout(gbl_mondayPanel);
			{
				mondayBarPanel = new BarPanel(Color.BLUE);
				mondayBarPanel.setBarHeight(0.3f);
				GridBagConstraints gbc_mondayBarPanel = new GridBagConstraints();
				gbc_mondayBarPanel.fill = GridBagConstraints.BOTH;
				gbc_mondayBarPanel.insets = new Insets(0, 0, 5, 0);
				gbc_mondayBarPanel.gridx = 0;
				gbc_mondayBarPanel.gridy = 0;
				mondayPanel.add(mondayBarPanel, gbc_mondayBarPanel);
			}
			{
				mondayTimePanel = new TimePanel();
				GridBagConstraints gbc_mondayTimePanel = new GridBagConstraints();
				gbc_mondayTimePanel.anchor = GridBagConstraints.NORTHWEST;
				gbc_mondayTimePanel.insets = new Insets(0, 0, 5, 0);
				gbc_mondayTimePanel.gridx = 0;
				gbc_mondayTimePanel.gridy = 1;
				mondayPanel.add(mondayTimePanel, gbc_mondayTimePanel);
			}
			{
				JLabel label = new JLabel("Monday");
				GridBagConstraints gbc_label = new GridBagConstraints();
				gbc_label.gridx = 0;
				gbc_label.gridy = 2;
				mondayPanel.add(label, gbc_label);
			}
		}
		{
			JPanel tuesdayPanel = new JPanel();
			tuesdayPanel.setBorder(new LineBorder(new Color(0, 0, 0)));
			contentPanel.add(tuesdayPanel);
			GridBagLayout gbl_tuesdayPanel = new GridBagLayout();
			gbl_tuesdayPanel.columnWidths = new int[]{80, 0};
			gbl_tuesdayPanel.rowHeights = new int[]{32, 0, 0, 0};
			gbl_tuesdayPanel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
			gbl_tuesdayPanel.rowWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
			tuesdayPanel.setLayout(gbl_tuesdayPanel);
			{
				tuesdayBarPanel = new BarPanel(Color.BLUE);
				tuesdayBarPanel.setBarHeight(0.55f);
				GridBagConstraints gbc_tuesdayBarPanel = new GridBagConstraints();
				gbc_tuesdayBarPanel.fill = GridBagConstraints.BOTH;
				gbc_tuesdayBarPanel.insets = new Insets(0, 0, 5, 0);
				gbc_tuesdayBarPanel.gridx = 0;
				gbc_tuesdayBarPanel.gridy = 0;
				tuesdayPanel.add(tuesdayBarPanel, gbc_tuesdayBarPanel);
			}
			{
				tuesdayTimePanel = new TimePanel();
				GridBagConstraints gbc_tuesdayTimePanel = new GridBagConstraints();
				gbc_tuesdayTimePanel.anchor = GridBagConstraints.NORTHWEST;
				gbc_tuesdayTimePanel.insets = new Insets(0, 0, 5, 0);
				gbc_tuesdayTimePanel.gridx = 0;
				gbc_tuesdayTimePanel.gridy = 1;
				tuesdayPanel.add(tuesdayTimePanel, gbc_tuesdayTimePanel);
			}
			{
				JLabel lblTuesday = new JLabel("Tuesday");
				GridBagConstraints gbc_lblTuesday = new GridBagConstraints();
				gbc_lblTuesday.gridx = 0;
				gbc_lblTuesday.gridy = 2;
				tuesdayPanel.add(lblTuesday, gbc_lblTuesday);
			}
		}
		{
			JPanel wednesdayPanel = new JPanel();
			wednesdayPanel.setBorder(new LineBorder(new Color(0, 0, 0)));
			contentPanel.add(wednesdayPanel);
			GridBagLayout gbl_wednesdayPanel = new GridBagLayout();
			gbl_wednesdayPanel.columnWidths = new int[]{80, 0};
			gbl_wednesdayPanel.rowHeights = new int[]{32, 0, 0, 0};
			gbl_wednesdayPanel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
			gbl_wednesdayPanel.rowWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
			wednesdayPanel.setLayout(gbl_wednesdayPanel);
			{
				wednesdayBarPanel = new BarPanel(Color.BLUE);
				wednesdayBarPanel.setBarHeight(0.7f);
				GridBagConstraints gbc_wednesdayBarPanel = new GridBagConstraints();
				gbc_wednesdayBarPanel.fill = GridBagConstraints.BOTH;
				gbc_wednesdayBarPanel.insets = new Insets(0, 0, 5, 0);
				gbc_wednesdayBarPanel.gridx = 0;
				gbc_wednesdayBarPanel.gridy = 0;
				wednesdayPanel.add(wednesdayBarPanel, gbc_wednesdayBarPanel);
			}
			{
				wednesdayTimePanel = new TimePanel();
				GridBagConstraints gbc_wednesdayTimePanel = new GridBagConstraints();
				gbc_wednesdayTimePanel.anchor = GridBagConstraints.NORTHWEST;
				gbc_wednesdayTimePanel.insets = new Insets(0, 0, 5, 0);
				gbc_wednesdayTimePanel.gridx = 0;
				gbc_wednesdayTimePanel.gridy = 1;
				wednesdayPanel.add(wednesdayTimePanel, gbc_wednesdayTimePanel);
			}
			{
				JLabel lblWednesday = new JLabel("Wednesday");
				GridBagConstraints gbc_lblWednesday = new GridBagConstraints();
				gbc_lblWednesday.gridx = 0;
				gbc_lblWednesday.gridy = 2;
				wednesdayPanel.add(lblWednesday, gbc_lblWednesday);
			}
		}
		{
			JPanel thursdayPanel = new JPanel();
			thursdayPanel.setBorder(new LineBorder(new Color(0, 0, 0)));
			contentPanel.add(thursdayPanel);
			GridBagLayout gbl_thursdayPanel = new GridBagLayout();
			gbl_thursdayPanel.columnWidths = new int[]{80, 0};
			gbl_thursdayPanel.rowHeights = new int[]{32, 0, 0, 0};
			gbl_thursdayPanel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
			gbl_thursdayPanel.rowWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
			thursdayPanel.setLayout(gbl_thursdayPanel);
			{
				thursdayBarPanel = new BarPanel(Color.BLUE);
				thursdayBarPanel.setBarHeight(0.2f);
				GridBagConstraints gbc_thursdayBarPanel = new GridBagConstraints();
				gbc_thursdayBarPanel.fill = GridBagConstraints.BOTH;
				gbc_thursdayBarPanel.insets = new Insets(0, 0, 5, 0);
				gbc_thursdayBarPanel.gridx = 0;
				gbc_thursdayBarPanel.gridy = 0;
				thursdayPanel.add(thursdayBarPanel, gbc_thursdayBarPanel);
			}
			{
				thursdayTimePanel = new TimePanel();
				GridBagConstraints gbc_thursdayTimePanel = new GridBagConstraints();
				gbc_thursdayTimePanel.anchor = GridBagConstraints.NORTHWEST;
				gbc_thursdayTimePanel.insets = new Insets(0, 0, 5, 0);
				gbc_thursdayTimePanel.gridx = 0;
				gbc_thursdayTimePanel.gridy = 1;
				thursdayPanel.add(thursdayTimePanel, gbc_thursdayTimePanel);
			}
			{
				JLabel lblThursday = new JLabel("Thursday");
				GridBagConstraints gbc_lblThursday = new GridBagConstraints();
				gbc_lblThursday.gridx = 0;
				gbc_lblThursday.gridy = 2;
				thursdayPanel.add(lblThursday, gbc_lblThursday);
			}
		}
		{
			JPanel panel = new JPanel();
			panel.setBorder(new LineBorder(new Color(0, 0, 0)));
			contentPanel.add(panel);
			GridBagLayout gbl_panel = new GridBagLayout();
			gbl_panel.columnWidths = new int[]{80, 0};
			gbl_panel.rowHeights = new int[]{32, 0, 0, 0};
			gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
			gbl_panel.rowWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
			panel.setLayout(gbl_panel);
			{
				fridayBarPanel = new BarPanel(Color.BLUE);
				fridayBarPanel.setBarHeight(0.7f);
				GridBagConstraints gbc_fridayBarPanel = new GridBagConstraints();
				gbc_fridayBarPanel.fill = GridBagConstraints.BOTH;
				gbc_fridayBarPanel.insets = new Insets(0, 0, 5, 0);
				gbc_fridayBarPanel.gridx = 0;
				gbc_fridayBarPanel.gridy = 0;
				panel.add(fridayBarPanel, gbc_fridayBarPanel);
			}
			{
				fridayTimePanel = new TimePanel();
				GridBagConstraints gbc_fridayTimePanel = new GridBagConstraints();
				gbc_fridayTimePanel.anchor = GridBagConstraints.NORTHWEST;
				gbc_fridayTimePanel.insets = new Insets(0, 0, 5, 0);
				gbc_fridayTimePanel.gridx = 0;
				gbc_fridayTimePanel.gridy = 1;
				panel.add(fridayTimePanel, gbc_fridayTimePanel);
			}
			{
				JLabel lblFriday = new JLabel("Friday");
				GridBagConstraints gbc_lblFriday = new GridBagConstraints();
				gbc_lblFriday.gridx = 0;
				gbc_lblFriday.gridy = 2;
				panel.add(lblFriday, gbc_lblFriday);
			}
		}
		{
			JPanel panel = new JPanel();
			panel.setBorder(new LineBorder(new Color(0, 0, 0)));
			contentPanel.add(panel);
			GridBagLayout gbl_panel = new GridBagLayout();
			gbl_panel.columnWidths = new int[]{80, 0};
			gbl_panel.rowHeights = new int[]{32, 0, 0, 0};
			gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
			gbl_panel.rowWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
			panel.setLayout(gbl_panel);
			{
				saturdayBarPanel = new BarPanel(Color.BLUE);
				saturdayBarPanel.setBarHeight(0.1f);
				GridBagConstraints gbc_saturdayBarPanel = new GridBagConstraints();
				gbc_saturdayBarPanel.fill = GridBagConstraints.BOTH;
				gbc_saturdayBarPanel.insets = new Insets(0, 0, 5, 0);
				gbc_saturdayBarPanel.gridx = 0;
				gbc_saturdayBarPanel.gridy = 0;
				panel.add(saturdayBarPanel, gbc_saturdayBarPanel);
			}
			{
				saturdayTimePanel = new TimePanel();
				GridBagConstraints gbc_saturdayTimePanel = new GridBagConstraints();
				gbc_saturdayTimePanel.anchor = GridBagConstraints.NORTHWEST;
				gbc_saturdayTimePanel.insets = new Insets(0, 0, 5, 0);
				gbc_saturdayTimePanel.gridx = 0;
				gbc_saturdayTimePanel.gridy = 1;
				panel.add(saturdayTimePanel, gbc_saturdayTimePanel);
			}
			{
				JLabel lblSaturday = new JLabel("Saturday");
				GridBagConstraints gbc_lblSaturday = new GridBagConstraints();
				gbc_lblSaturday.gridx = 0;
				gbc_lblSaturday.gridy = 2;
				panel.add(lblSaturday, gbc_lblSaturday);
			}
		}
		{
			JPanel panel = new JPanel();
			panel.setBorder(new LineBorder(new Color(0, 0, 0)));
			contentPanel.add(panel);
			GridBagLayout gbl_panel = new GridBagLayout();
			gbl_panel.columnWidths = new int[]{80, 0};
			gbl_panel.rowHeights = new int[]{32, 0, 0, 0};
			gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
			gbl_panel.rowWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
			panel.setLayout(gbl_panel);
			{
				sundayBarPanel = new BarPanel(Color.BLUE);
				sundayBarPanel.setBarHeight(0.7f);
				GridBagConstraints gbc_sundayBarPanel = new GridBagConstraints();
				gbc_sundayBarPanel.fill = GridBagConstraints.BOTH;
				gbc_sundayBarPanel.insets = new Insets(0, 0, 5, 0);
				gbc_sundayBarPanel.gridx = 0;
				gbc_sundayBarPanel.gridy = 0;
				panel.add(sundayBarPanel, gbc_sundayBarPanel);
			}
			{
				sundayTimePanel = new TimePanel();
				GridBagConstraints gbc_sundayTimePanel = new GridBagConstraints();
				gbc_sundayTimePanel.anchor = GridBagConstraints.NORTHWEST;
				gbc_sundayTimePanel.insets = new Insets(0, 0, 5, 0);
				gbc_sundayTimePanel.gridx = 0;
				gbc_sundayTimePanel.gridy = 1;
				panel.add(sundayTimePanel, gbc_sundayTimePanel);
			}
			{
				JLabel lblSunday = new JLabel("Sunday");
				GridBagConstraints gbc_lblSunday = new GridBagConstraints();
				gbc_lblSunday.gridx = 0;
				gbc_lblSunday.gridy = 2;
				panel.add(lblSunday, gbc_lblSunday);
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				okButton.addActionListener(new ActionListener()
				{
					@Override
					public void actionPerformed(ActionEvent arg0) {
						System.out.println("Pressed OK");
						status = Status.OK;
						ChangeWeeklyDetailsDialog3.this.dispose();
					}	
				});
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener()
				{
					@Override
					public void actionPerformed(ActionEvent arg0) {
						status = Status.CANCEL;
						ChangeWeeklyDetailsDialog3.this.dispose();
					}	
				}
				);
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}

		}
		
		timePanels.add(mondayTimePanel);
		timePanels.add(tuesdayTimePanel);
		timePanels.add(wednesdayTimePanel);
		timePanels.add(thursdayTimePanel);
		timePanels.add(fridayTimePanel);
		timePanels.add(saturdayTimePanel);
		timePanels.add(sundayTimePanel);

		TimePanelListener tpListener = new TimePanelListener()
		{
			@Override
			public void timePanelChanged(TimePanel tp) {
				ChangeWeeklyDetailsDialog3.this.updateBarPanels();
			}	
		};
		
		mondayTimePanel.addTimePanelListener(tpListener);
		tuesdayTimePanel.addTimePanelListener(tpListener);
		wednesdayTimePanel.addTimePanelListener(tpListener);
		thursdayTimePanel.addTimePanelListener(tpListener);
		fridayTimePanel.addTimePanelListener(tpListener);
		saturdayTimePanel.addTimePanelListener(tpListener);
		sundayTimePanel.addTimePanelListener(tpListener);		
		
		barPanels.add(mondayBarPanel);
		barPanels.add(tuesdayBarPanel);
		barPanels.add(wednesdayBarPanel);
		barPanels.add(thursdayBarPanel);
		barPanels.add(fridayBarPanel);
		barPanels.add(saturdayBarPanel);
		barPanels.add(sundayBarPanel);

		this.pack();
	}
	
	private void updateBarPanels()
	{
		MinTime totalTime = new MinTime(0,0);
		
		Week week = getTimes();
		System.out.println("Upadting");
		
		for(Day day : Day.values())
		{
			MinTime time = week.get(day);
			System.out.println("Adding " + day.name() + ", " + time);
			totalTime = totalTime.add(time);
		}
		
		for(Day day : Day.values())
		{
			MinTime time = week.get(day);
			float h = (float)time.getAsMinutes() / (float)totalTime.getAsMinutes();
			barPanels.get(day.getDayNr()).setBarHeight(h);
			//barPanels.get(day.getDayNr()).repaint();
		}		
		invalidate();
		validate();
		repaint();
	}
	
	public void setTimes(Week week)
	{
		MinTime totalTime = new MinTime(0,0);
		
		for(Day day : Day.values())
		{
			MinTime time = week.get(day);
			timePanels.get(day.getDayNr()).setTime(time);
//			totalTime = timePanels.get(day.getDayNr()).getTime();
			totalTime = totalTime.add(time);
		}
		
		for(Day day : Day.values())
		{
			MinTime time = week.get(day);
			float h = (float)time.getAsMinutes() / (float)totalTime.getAsMinutes();
			System.out.println(day.name() + ", " + h);
			barPanels.get(day.getDayNr()).setBarHeight(h);
		}
	}
	
	public Week getTimes()
	{
		Week week = new Week();
		for(Day day : Day.values())
		{
			MinTime time = timePanels.get(day.getDayNr()).getTime();
			week.set(Day.values()[day.getDayNr()], time);
		}
		return week;
	}
	
	public void disableDay(Week.Day day)
	{
		timePanels.get(day.getDayNr()).disablePanel();
		updateBarPanels();
	}
	

}
